package com.hsteach.service.visit;

import java.util.List;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import com.hsteach.common.exception.ServiceException;
import com.hsteach.common.utils.DateUtils;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.enums.VisitDataTypeEnum;
import com.hsteach.framework.feign.client.xgk.dto.ret.PersonInfoRet;
import com.hsteach.service.third.XgkService;
import com.hsteach.web.controller.visit.dto.UpdateTagsDto;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hsteach.dao.mapper.visit.VisitInterestTagMapper;
import com.hsteach.dao.domain.visit.VisitInterestTag;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生重点关注项Service业务层处理
 * 
 * @author fanxquer
 * @date 2023-07-21
 */
@Service
public class VisitInterestTagService
{
    @Autowired
    private VisitInterestTagMapper visitInterestTagMapper;

    @Autowired
    private XgkService xgkService;

    /**
     * 查询学生重点关注项
     * 
     * @param id 学生重点关注项主键
     * @return 学生重点关注项
     */
    public VisitInterestTag selectVisitInterestTagById(Long id)
    {
        return visitInterestTagMapper.selectVisitInterestTagById(id);
    }

    /**
     * 查询学生重点关注项列表
     * 
     * @param visitInterestTag 学生重点关注项
     * @return 学生重点关注项
     */
    public List<VisitInterestTag> selectVisitInterestTagList(VisitInterestTag visitInterestTag)
    {
        return visitInterestTagMapper.selectVisitInterestTagList(visitInterestTag);
    }

    /**
     * 新增学生重点关注项
     * 
     * @param visitInterestTag 学生重点关注项
     * @return 结果
     */
    public int insertVisitInterestTag(VisitInterestTag visitInterestTag)
    {
        visitInterestTag.setCreateTime(DateUtils.getNowDate());
        return visitInterestTagMapper.insertVisitInterestTag(visitInterestTag);
    }

    /**
     * 修改学生重点关注项
     * 
     * @param visitInterestTag 学生重点关注项
     * @return 结果
     */
    public int updateVisitInterestTag(VisitInterestTag visitInterestTag)
    {
        visitInterestTag.setUpdateTime(DateUtils.getNowDate());
        return visitInterestTagMapper.updateVisitInterestTag(visitInterestTag);
    }

    /**
     * 修改学生重点关注项
     *
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateTags(UpdateTagsDto dto)
    {
        List<PersonInfoRet> personInfo = xgkService.getStudentByPersonIds(Lists.newArrayList(dto.getPersonId()));
        if (CollectionUtils.isEmpty(personInfo)) {
            throw new ServiceException("未查询到基础信息");
        }
        PersonInfoRet personInfoRet = personInfo.get(0);
        // 先更新基础
        visitInterestTagMapper.deleteByPersonIdAndType(dto.getPersonId(), VisitDataTypeEnum.STUDENT.getCode(), null);
        List<VisitInterestTag> studentTags = dto.getTags().stream().map(e -> {
            VisitInterestTag tag = new VisitInterestTag();
            tag.setTag(e);
            tag.setDataType(VisitDataTypeEnum.STUDENT.getCode());
            tag.setCreateBy(SecurityUtils.getAuthInfo().getPersonId());
            tag.setCreateByName(SecurityUtils.getAuthInfo().getName());
            tag.setPersonId(dto.getPersonId());
            tag.setClassId(personInfoRet.getCurrentClass());
            return tag;
        }).collect(Collectors.toList());
        // 有记录则加入记录更新
        if (dto.getRecordId() != null) {
            visitInterestTagMapper.deleteByPersonIdAndType(dto.getPersonId(), VisitDataTypeEnum.RECORD.getCode(), dto.getRecordId());
            List<VisitInterestTag> recordTags = dto.getTags().stream().map(e -> {
                VisitInterestTag tag = new VisitInterestTag();
                tag.setTag(e);
                tag.setVisitRecordId(dto.getRecordId());
                tag.setDataType(VisitDataTypeEnum.RECORD.getCode());
                tag.setCreateBy(SecurityUtils.getAuthInfo().getPersonId());
                tag.setCreateByName(SecurityUtils.getAuthInfo().getName());
                tag.setPersonId(dto.getPersonId());
                tag.setClassId(personInfoRet.getCurrentClass());
                return tag;
            }).collect(Collectors.toList());
            studentTags.addAll(recordTags);
        }

        if (studentTags.size() == 0) {
            return 1;
        }

        return visitInterestTagMapper.batchInsert(studentTags);
    }

    /**
     * 批量删除学生重点关注项
     * 
     * @param ids 需要删除的学生重点关注项主键
     * @return 结果
     */
    public int deleteVisitInterestTagByIds(Long[] ids)
    {
        return visitInterestTagMapper.deleteVisitInterestTagByIds(ids);
    }

    /**
     * 删除学生重点关注项信息
     * 
     * @param id 学生重点关注项主键
     * @return 结果
     */
    public int deleteVisitInterestTagById(Long id)
    {
        return visitInterestTagMapper.deleteVisitInterestTagById(id);
    }
}
